package com.ruoyi.fjtseac.service

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.ruoyi.fjtseac.domain.dto.ApplicationFormDto
import com.ruoyi.fjtseac.domain.po.ApplicationForm
import com.ruoyi.fjtseac.mapper.ApplicationFormMapper
import com.ruoyi.fjtseac.service.impl.ApplicationFormServiceImpl
import spock.lang.Specification
import spock.lang.Unroll


class ApplicatonFormServiceTest extends Specification{
    def applicationFormMapper = Mock(ApplicationFormMapper)
    def applicationFormServiceImpl = new ApplicationFormServiceImpl(applicationFormMapper: applicationFormMapper)


    @Unroll
    def "add volunteer apply"(){
        given:
        applicationFormMapper.selectOne(_ as QueryWrapper<ApplicationForm>)>>form
        applicationFormMapper.insertApplicationForm(_ as ApplicationForm)>>res1
        applicationFormMapper.deleteApplicationFormById(_ as Long)>>res2

        when:
        def ans = applicationFormServiceImpl.insertApplicationForm(applicationFormDto)

        then:
        with(ans){
            msg == result
        }

        where:
        applicationFormDto      |             form            |        res1           |     res2    | result
        new ApplicationFormDto(name: "啊",openId: "123456")| null    |    1   |     0    |"申请成功"
        new ApplicationFormDto(name: "吧",openId: "123456")|new ApplicationForm(name: "吧",openId: "123456",flag: 1)|    1   | 1 |"你已提交过申请"
        new ApplicationFormDto(name: "从",openId: "123456")|new ApplicationForm(name: "从",openId: "123456",flag: 2)| 0 |0|"您已经成为志愿者了"


    }

    @Unroll
    def "select one By openID"(){
        given:
        applicationFormMapper.selectOne(_ as QueryWrapper<ApplicationForm>)>> form

        when:
        def ans = applicationFormServiceImpl.selectOneByOpenId(openId)

        then:
        with(ans){
            msg == result
        }

        where:
        openId| form    |   result
        "123"| null |"您还未申请志愿者"
        "123"|new ApplicationForm(name: "啊",openId: "123")|"操作成功"
    }


    @Unroll
    def "refuse volunteer apply"(){
        given:
        applicationFormMapper.updateById(_ as ApplicationForm)>> num

        when:
         def ans = applicationFormServiceImpl.refuse(applicationFormDto)

        then:
        with(ans){
            msg == result
        }

        where:
        applicationFormDto |         num        | result
        new ApplicationFormDto(id: 1,reason: "no") |  1  |"操作成功"
        new ApplicationFormDto(id: 1,reason: "no") |  0  |"操作失败"
        new ApplicationFormDto(id: 1,reason: "<script></script>") |  0  |"拒绝理由不应该含有脚本语言"

    }
}
